package com.yh.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yh.bean.BuyGoods;
import com.yh.bean.CollectDetail;
import com.yh.bean.Game;
import com.yh.bean.GameClass;
import com.yh.bean.GameZone;
import com.yh.bean.Game_GameZone;
import com.yh.bean.Game_GameZone_GoodsClass;
import com.yh.bean.Goods;
import com.yh.bean.GoodsClass;
import com.yh.bean.Inorder;
import com.yh.bean.User;
import com.yh.service.UserCoreService;


@Controller
public class UserCoreController {
	@Autowired
	private UserCoreService userService;
	//用户登陆（测试）
	@RequestMapping("testLogin")
	public void testLogin(HttpServletRequest request){
		HttpSession session=request.getSession();
		User user=new User();
		user.setPhoto("images/头像2.jpg");
		user.setUid(7);
		user.setUname("华山剑宗");
		user.setStatus(2);
		session.setAttribute("user", user);
		System.out.println(user.getUid());
	}
	//用户退出登陆
	@RequestMapping("quitLogin.do")

	public String quitLogin(HttpServletRequest request){
		System.out.println("==========注销===============");
		HttpSession session=request.getSession();
		session.invalidate();
		return "testlogin1";
	}
	//用户充值
	@RequestMapping("addumoney.do")
	@ResponseBody
	public double addUmoney(double umoney,HttpServletRequest request){
		User user=(User) request.getSession().getAttribute("user");
		Map<String,Object> map=new HashMap<>();
		map.put("uid", user.getUid());
		map.put("umoney", umoney);
		double returnUmoney=userService.addUmoney(map);
		return returnUmoney;
	}
	//查看用户信息
	@RequestMapping("queryUserInfo.do")
	public String queryUserInfo(HttpServletRequest request,ModelMap modelMap){
		User user=(User) request.getSession().getAttribute("user");
		User returnUser=userService.queryUserInfo(user.getUid());
		modelMap.put("userInfo", returnUser);
		return "lhtml/userInfo";
	}
	//用户签到
	@RequestMapping("userSign.do")
	@ResponseBody
	public boolean userSign(HttpServletRequest request){
		User user=(User)request.getSession().getAttribute("user");
		System.out.println("*********************************************"+user.getUid());
		return userService.sign(user.getUid());
	}
	//修改用户信息
	@RequestMapping("updateUserInfo.do")
	public String updateUserInfo(User user,MultipartFile photo1,HttpServletRequest request) throws IOException{
		Boolean bool=false;
		User user1=(User)request.getSession().getAttribute("user");
		user.setUid(user1.getUid());
		if(photo1.getOriginalFilename()==null || photo1.getOriginalFilename().equals("")){
			System.out.println("*******************"+photo1.getOriginalFilename());
			bool=userService.updateUserInfo(user);
			System.out.println("////////////////"+bool);
		}else{
			String realpath=request.getServletContext().getRealPath("/images");
			String imagePath="images/"+photo1.getOriginalFilename();
			String uploadpath=realpath+"/"+photo1.getOriginalFilename();
			System.out.println(uploadpath);
			System.out.println("***********************"+user.getUname());
			user.setPhoto(imagePath);
			FileUtils.copyInputStreamToFile(photo1.getInputStream(),new File(uploadpath));         //事务原因不能try/catch
			bool=userService.updateUserInfo(user);
		}
		System.out.println(bool);
		if(bool){
			return "redirect:queryUserInfo.do";
		}
		return null;
	}
	//用户修改登录密码
	@RequestMapping("updatePassword.do")
	@ResponseBody
	public boolean updatePassword(String newPassword,HttpServletRequest request){
		User user=(User) request.getSession().getAttribute("user");
		User user1=new User();
		user1.setUid(user.getUid());
		user1.setPassword(newPassword);
		return userService.updateUserPassword(user1);
	}
	//用户修改支付密码
	@RequestMapping("updatePayPassword.do")
	@ResponseBody
	public boolean updatePayPassword(String newPayPassword,HttpServletRequest request){
		User user=(User) request.getSession().getAttribute("user");
		User user1=new User();
		user1.setUid(user.getUid());
		user1.setPassword(newPayPassword);
		return userService.updateUserPayPassword(user1);
	}
	//用户查看自己已上架商品
	@RequestMapping("queryGoods.do")
	@ResponseBody
	public Map<String, Object> queryGoods(int currentPage,int pageSize,Goods goods,HttpServletRequest request){
		User user=(User) request.getSession().getAttribute("user");
		int uid=user.getUid();
		goods.setU_Id(uid);
		Page page=PageHelper.startPage(currentPage, pageSize);
		System.out.println("****************************goodsStatus:"+goods.getStatus());
		List<Goods> goodsList=userService.queryGoods(goods);
		Map<String,Object> map=new HashMap<>();
		map.put("pages", page.getPages());
		map.put("total",page.getTotal());
		map.put("goodsList", goodsList);
		return map;
	}
	//用户下架商品
	@RequestMapping("deleteGoods.do")
	@ResponseBody
	public Boolean deleteGoods(String gid){
		String[] strArray={gid};
		boolean bool=userService.deleteGoods(strArray);
		return bool;
	}
	//用户下架所有选中商品
		@RequestMapping("deleteCheckGoods.do")
		@ResponseBody
		public Boolean deleteCheckGoods(String strGidArr){
			String[] strArray=strGidArr.split(",");
			boolean bool=userService.deleteGoods(strArray);
			return bool;
		}
	//获取游戏分区表
		@RequestMapping("queryGameClass.do")
		@ResponseBody
		public List<GameClass> queryGameClass(){
			return userService.queryGameClass();
		}
	//获取游戏表
	@RequestMapping("queryGames.do")
	@ResponseBody
	public List<Game> queryGame(int gcId){
		return userService.queryGame(gcId);
	}
	//获取游戏分区表
	@RequestMapping("queryGameZone.do")
	@ResponseBody
	public List<GameZone> queryGameZone(int gId){
		return userService.queryGameZone(gId);
	}
	//获取游戏表_游戏分区表id
	@RequestMapping("queryGame_GameZone.do")
	@ResponseBody
	public int queryGame_GameZoneId(Game_GameZone ggz,String gzId){
		int gz_id=0;
		if(gzId!=null && !gzId.equals("")){
			ggz.setGz_id(Integer.parseInt(gzId));
		}else{
			ggz.setGz_id(gz_id);
		}
		return userService.queryGame_GameZoneId(ggz);
	}
	//获取商品分类表
	@RequestMapping("queryGoodsClass.do")
	@ResponseBody
	public List<GoodsClass> queryGoodsClass(int ggzId){
		return userService.queryGoodsClass(ggzId);
	}
	//商品上架
	@RequestMapping("addToGoods.do")
	@ResponseBody
	public boolean addToGoods(Game_GameZone_GoodsClass ggg,Goods goods,MultipartFile photo1,HttpServletRequest request){
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");
		boolean bool=false;
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		goods.setGtime(sdf.format(date));
		goods.setU_Id(user.getUid());
		goods.setStatus(-1);
		Map<String,Object> map=new HashMap<>();
		//判断图片是否为空
		if(photo1!=null){
			String realpath=request.getServletContext().getRealPath("/images");
			String imagePath="images/"+photo1.getOriginalFilename();
			String uploadpath=realpath+"/"+photo1.getOriginalFilename();
			goods.setGphoto(imagePath);
			try {
				FileUtils.copyInputStreamToFile(photo1.getInputStream(),new File(uploadpath));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			map.put("goods", goods);
			map.put("game_gameZone_goodsClass", ggg);
			bool=userService.addGoods(map);
			return bool;
		}
		map.put("goods", goods);
		map.put("game_gameZone_goodsClass", ggg);
		bool=userService.addGoods(map);
		return bool;
	}
	//查看已购商品
	@RequestMapping("queryBuyGoods.do")
	@ResponseBody
	public Map<String,Object> queryBuyGoods(int currentPage,int pageSize,BuyGoods bgoods,HttpServletRequest request){
		User user=(User) request.getSession().getAttribute("user");
		int uid=user.getUid();
		bgoods.setU_id(uid);
		Page page=PageHelper.startPage(currentPage, pageSize);
		List<BuyGoods> bgoodsList=userService.queryBuyGoods(bgoods);
		Map<String,Object> map=new HashMap<>();
		map.put("pages", page.getPages());
		map.put("total",page.getTotal());
		map.put("bgoodsList", bgoodsList);
		return map;
	}
	//查看已购积分商品
		@RequestMapping("queryInorder.do")
		@ResponseBody
		public Map<String,Object> queryInorder(int currentPage,int pageSize,HttpServletRequest request){
			User user=(User) request.getSession().getAttribute("user");
			int uid=user.getUid();
			Inorder inorder=new Inorder();
			inorder.setU_id(uid);
			Page page=PageHelper.startPage(currentPage, pageSize);
			List<Inorder> inorderList=userService.queryInorder(inorder);
			Map<String,Object> map=new HashMap<>();
			map.put("pages", page.getPages());
			map.put("total",page.getTotal());
			map.put("inorderList", inorderList);
			return map;
		}
	//查看我的收藏
		@RequestMapping("queryCollectDetail.do")
		@ResponseBody
		public Map<String,Object> queryCollectDetail(int currentPage,int pageSize,HttpServletRequest request){
			User user=(User) request.getSession().getAttribute("user");
			int uid=user.getUid();
			Page page=PageHelper.startPage(currentPage, pageSize);
			List<CollectDetail> collList=userService.queryCollectDetail(uid);
			for(CollectDetail coll:collList){
				Goods goods=coll.getGoods();
				System.out.println("**************************goodsName:"+goods.getGname());
			}
			Map<String,Object> map=new HashMap<>();
			map.put("pages", page.getPages());
			map.put("total",page.getTotal());
			map.put("collList", collList);
			return map;
		}
	//取消收藏
		@RequestMapping("deleteCollectDetail.do")
		@ResponseBody
		public boolean deleteCollectDetail(int cdId){
			return userService.deltecollectDetail(cdId);
		}

	//浏览记录
		@RequestMapping("queryBrowseRecords.do")
		public String queryBrowseRecords(HttpServletRequest request){
			User user=(User) request.getSession().getAttribute("user");
			List<Goods> goodsList=userService.queryGoodsByBrowseRecords(user.getUid());
			System.out.println("--------------goodsList"+goodsList.toString());
			request.setAttribute("goodsList",goodsList);
			return "lhtml/browseRecords";
		}
	//添加游戏分类
		@RequestMapping("addGameClass.se")
		@ResponseBody
		public boolean addGameClass(String gcName){
			List<GameClass> gcList=userService.queryGameClass();
			for(GameClass gc:gcList){
				if(gc.getGcName().equals(gcName)){
					return false;
				}
			}
			return userService.addGameClass(gcName);
		}
	//添加游戏
		@RequestMapping("addGame.se")
		@ResponseBody
		public boolean addGame(Game game){
			List<Game> gameList=userService.queryGame(game.getGc_Id());
			for(Game ga:gameList){
				if(ga.getGameName().equals(game.getGameName())){
					return false;
				}
			}
			return userService.addGame(game);
		}
	//添加游戏分区
		@RequestMapping("addGameZone.se")
		@ResponseBody
		public boolean addGameZone(String game_id,String gZName){
			int gameid=Integer.parseInt(game_id);
			if(gZName==null || gZName==""){
				Game_GameZone ggz=new Game_GameZone();
				ggz.setGame_id(gameid);
				Game_GameZone reggz=userService.queryGame_GameZoneIdToBean(ggz);
				//判断通过此游戏id得到的关联表的id存不存在
				if(reggz!=null){
					return false;
				}
				return userService.addGame_GameZone(ggz);
			}
			List<GameZone> gameZoneList=userService.queryGameZone(gameid);
			for(GameZone gz:gameZoneList){
				if(gZName.equals(gz.getGzName())){
					return false;
				}
			}
			GameZone gameZone=new GameZone();
			gameZone.setGzName(gZName);
			boolean bool1=userService.addGameZone(gameZone);
			int gzId=gameZone.getGzId();
			Game_GameZone ggz=new Game_GameZone();
			ggz.setGame_id(gameid);
			ggz.setGz_id(gzId);
			Boolean bool2=userService.addGame_GameZone(ggz);
			if(bool1==true && bool2==true){
				return true;
			}
			return false;
		}
	//添加商品分类
		@RequestMapping("addGoodsClass.se")
		@ResponseBody
		public boolean addGoodsClass(int ggzId,String gcName){
			List<GoodsClass> gcList=userService.queryGoodsClass(ggzId);
			for(GoodsClass gc:gcList){
				if(gcName.equals(gc.getGcName())){
					return false;
				}
			}
			Map<String,Object> map=new HashMap<>();
			GoodsClass gc=new GoodsClass();
			gc.setGcName(gcName);
			Game_GameZone_GoodsClass ggg=new Game_GameZone_GoodsClass();
			ggg.setG_gz_id(ggzId);
			map.put("goodsClass", gc);
			map.put("ggg", ggg);
			return userService.addGoodsClass(map);
		}
}
